package com.tinghua.configuration;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQQueueConfig {
    //队列10msTTL
    public static final String QUEUE_NAME_A = "QA";
    //队列B20msTTL
    public static final String QUEUE_NAME_B = "QB";
    //队列C参数形式TTL
    public static final String QUEUE_NAME_C = "QC";
    //队列D死信队列
    public static final String QUEUE_NAME_D = "QD";
    //交换机A面对生产者
    public static final String EXCHANGE_NAME_A = "EA";
    //交换机B转发死信队列
    public static final String EXCHANGE_NAME_B = "EB";

    @Bean("queueA")
    public Queue declarationQA(){
        Map<String, Object> map = new HashMap<>();
        // 绑定死信队列的参数配置
        map.put("x-dead-letter-exchange", EXCHANGE_NAME_B);
        map.put("x-dead-letter-routing-key", "E");
        map.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_NAME_A).withArguments(map).build();
    }

    @Bean("queueB")
    public Queue declarationQB(){
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", EXCHANGE_NAME_B);
        map.put("x-dead-letter-routing-key", "E");
        map.put("x-message-ttl", 20000);
        return QueueBuilder.durable(QUEUE_NAME_B).withArguments(map).build();
    }

    @Bean("queueC")
    public Queue declarationQC(){
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", EXCHANGE_NAME_B);
        map.put("x-dead-letter-routing-key", "E");
        return QueueBuilder.durable(QUEUE_NAME_C).withArguments(map).build();
    }

    // 死信队列与其他队列的定义方式没区别
    @Bean("queueD")
    public Queue declarationQD(){
        return QueueBuilder.durable(QUEUE_NAME_D).build();
    }

    @Bean("exchangeA")
    public DirectExchange declarationEA(){
        return new DirectExchange(EXCHANGE_NAME_A);
    }

    @Bean("exchangeB")
    public DirectExchange declarationEB(){
        return new DirectExchange(EXCHANGE_NAME_B);
    }

    @Bean
    public Binding queueABindingExchangeA(@Qualifier("queueA") Queue queueA,
                                          @Qualifier("exchangeA") DirectExchange exchangeA){
        return BindingBuilder.bind(queueA).to(exchangeA).with("EA");
    }

    @Bean
    public Binding queueBBindingExchangeA(@Qualifier("queueB") Queue queueB,
                                          @Qualifier("exchangeA") DirectExchange exchangeA){
        return BindingBuilder.bind(queueB).to(exchangeA).with("EB");
    }

    @Bean
    public Binding queueCBindingExchangeA(@Qualifier("queueC") Queue queueC,
                                          @Qualifier("exchangeA") DirectExchange exchangeA){
        return BindingBuilder.bind(queueC).to(exchangeA).with("EC");
    }

    @Bean
    public Binding queueDBindingExchangeB(@Qualifier("queueD") Queue queueD,
                                          @Qualifier("exchangeB") DirectExchange exchangeB){
        return BindingBuilder.bind(queueD).to(exchangeB).with("E");
    }
}
